package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.domain.Commodity;
import com.mycompany.myapp.service.CartItemService;
import com.mycompany.myapp.domain.CartItem;
import com.mycompany.myapp.repository.CartItemRepository;
import com.mycompany.myapp.service.CommodityService;
import com.mycompany.myapp.service.dto.CartItemDTO;
import com.mycompany.myapp.service.mapper.CartItemMapper;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import com.mycompany.myapp.web.rest.utils.TypeUtils;
import com.mycompany.myapp.web.rest.vm.CartItemVM;
import org.checkerframework.checker.units.qual.C;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link CartItem}.
 */
@Service
@Transactional
public class CartItemServiceImpl implements CartItemService {

    private final Logger log = LoggerFactory.getLogger(CartItemServiceImpl.class);

    private final CartItemRepository cartItemRepository;

    private final CartItemMapper cartItemMapper;

    private final CommodityService commodityService;

    public CartItemServiceImpl(CartItemRepository cartItemRepository, CartItemMapper cartItemMapper,
                               CommodityService commodityService) {
        this.cartItemRepository = cartItemRepository;
        this.cartItemMapper = cartItemMapper;
        this.commodityService = commodityService;
    }

    /**
     * Save a cartItem.
     *
     * @param cartItemDTO the entity to save.
     * @return the persisted entity.
     */
    @Override
    public CartItemDTO save(CartItemDTO cartItemDTO) {
        log.debug("Request to save CartItem : {}", cartItemDTO);
        CartItem cartItem = cartItemMapper.toEntity(cartItemDTO);
        cartItem = cartItemRepository.save(cartItem);
        return cartItemMapper.toDto(cartItem);
    }

    /**
     * Get all the cartItems.
     *
     * @return the list of entities.
     */
    @Override
    @Transactional(readOnly = true)
    public List<CartItemDTO> findAll() {
        log.debug("Request to get all CartItems");
        return cartItemRepository.findAll().stream()
            .map(cartItemMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    /**
     * Get one cartItem by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<CartItemDTO> findOne(Long id) {
        log.debug("Request to get CartItem : {}", id);
        return cartItemRepository.findById(id)
            .map(cartItemMapper::toDto);
    }

    /**
     * Delete the cartItem by id.
     *
     * @param id the id of the entity.
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete CartItem : {}", id);
        cartItemRepository.deleteById(id);
    }


    public List<CartItemDTO> getCartItemByCartId(Long cartId){
        return cartItemRepository.findByCartId(cartId).stream()
            .map(cartItemMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    public ResultObj addCartItem(CartItemVM cartItemVM){
        if(!TypeUtils.isEmpty(cartItemVM)){
            CartItemDTO cartItemDTO = new CartItemDTO();
            cartItemDTO.setCartId(cartItemVM.getCartId());
            cartItemDTO.setCommodity(commodityService.findCommodityById(cartItemVM.getCommodityId()));
            cartItemDTO.setCount(cartItemVM.getCount());
            save(cartItemDTO);
            return ResultObj.backInfo(true,200,"添加成功",null);
        }
        return ResultObj.backInfo(true,200,"添加失败",null);
    }



}
